<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>智慧园区-登录</h1>
      <el-card shadow="never" class="login-card">
        <!-- 登录表单 -->
        <el-form ref="loginFormRef" label-position="top" :model="loginForm" :rules="rules">
          <el-form-item label="账号" prop="username">
            <el-input v-model="loginForm.username" type="text" placeholder="请输入账号" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" show-password />
          </el-form-item>
          <el-form-item label="模型uuid">
            <el-input v-model="loginForm.uuid" type="password" placeholder="请输入uuid" show-password />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="loginForm.isAgree">记住我</el-checkbox>
          </el-form-item>
          <el-button style="width:350px" type="primary" @click="login">登录</el-button>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { FORMDATA_KEY } from '@/constants/KEY'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        uuid: '',
        isAgree: false
      },
      rules: {
        username: [
          { required: true, message: '账号不能为空' }
        ],
        password: [
          { required: true, message: '密码不能为空' }
        ]
      }
    }
  },
  created() {
    const obj = JSON.parse(localStorage.getItem(FORMDATA_KEY))
    if (obj) {
      this.loginForm = obj
    }
  },
  methods: {
    async login() {
      await this.$refs.loginFormRef.validate()
      const { username, password } = this.loginForm
      this.$store.dispatch('user/login', { username, password })
      if (this.loginForm.isAgree) {
        localStorage.setItem(FORMDATA_KEY, JSON.stringify(this.loginForm))
      } else {
        localStorage.removeItem(FORMDATA_KEY)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

.login-container {
  height: 100vh;
  display: flex;
  align-items: stretch;

  .logo {
    flex: 3;
    background: url('../../assets//common/loginBg.png') no-repeat center / cover;
    border-top-right-radius: 60px;
  }

  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;

    .el-card {
      border: none;
      padding: 0;
    }

    h1 {
      padding-left: 20px;
      font-size: 24px;
    }

    ::v-deep .el-input {
      width: 350px;
      height: 44px;

      .el-input__inner {
        background: #f4f5fb;
      }
    }

    ::v-deep .el-checkbox {
      color: #606266;
    }
  }
}
</style>
